﻿var users = {};
users["test"] = "test2013";

function showWelcome() {
    $("#welcome").show();
    $("#loginMenu").hide();
    $("#Signup").hide();
}

function showLogin() {
    $("#welcome").hide();
    $("#loginMenu").show();
    $("#Signup").hide();
}

function showSignup() {
    $("#welcome").hide();
    $("#loginMenu").hide();
    $("#Signup").show();
}

function addUser() {
    var username = $('#txbSignupUsername').val();
    var password = $('#txbSignupPassword').val();
    users[username] = password;
}

function checkUser() {
    var username = $('#txbUserName').val();
    var password = $('#txbPassword').val();
    if (users[username] == password) {
        $('#loginMenu').hide();
        $('#pacmanInitialization').show();
        $('#playerName').val(username);
    }
    else {
        window.alert("Username or Password are invalid");
    }
}

function checkSignupForm() {
    var flag = true;
    $('#userIsEmpty').hide();
    $('#passwordIsEmpty').hide();
    $('#passwordIsLess').hide();
    $('#lastnameIsEmpty').hide();
    $('#firstnameIsEmpty').hide();

    var username = $('#txbSignupPassword').val();
    if(username.length < 1) {
        $('#userIsEmpty').show();
        flag = false;
    }

    var password = $('#txbSignupPassword').val();
    if (password.length < 1) {
        $('#passwordIsEmpty').show();
        flag = false;
    }
    else {
        var passw = /^(?=.*[0-9])[a-zA-Z0-9!@#$%^&*]{8,16}$/;
        if (!password.match(passw)) {
            $('#passwordIsLess').show();
            flag = false;
        }
    }

    var firstname = $('#firstName').val();
    if (firstname.length < 1) {
        $('#firstnameIsEmpty').show();
        flag = false;
    }

    var lastname = $('#lastName').val();
    if (lastname.length < 1) {
        $('#lastnameIsEmpty').show();
        flag = false;
    }

    return flag;
}

function showPacmanGameScreen() {
    $('#pacmanInitialization').hide();
    $('#pacmanGame').show();
    Start();
}

function checkVacinity(board, i, j, number) {
    var oppositeNumber;
    
    oppositeNumber = (number == 3) ? 4 : 3;
    try {
        if (i > 0 && j > 0 && (board[i - 1][j - 1] == oppositeNumber || board[i - 1][j - 1] == number)) {
            return false;
        }

        if (j > 0 && (board[i][j - 1] == oppositeNumber || board[i][j - 1] == number)) {
            return false;
        }

        if (i > 0 && (board[i - 1][j] == oppositeNumber || board[i - 1][j] == number)) {
            return false;
        }

    }catch (e) {
        return true;
    }

    return true;
}

function checkForMoster(monsterArray ,monster,i,j, numOfMonsters) {
    for(var k =0; k < numOfMonsters; k++) {
        if((k != monster)) {
            if(monsterArray[k].position.i == i &&
            monsterArray[k].position.j == j) {
                return false;
            }
        }
    }
    return true;
}